package com.hq.fxh.No2_BBS.Adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hq.fxh.Modle.PlateListModle;
import com.hq.fxh.R;
import com.hq.fxh.Utils.DensityUtils;
import com.hq.fxh.Utils.GlideUtils;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.SystemUtils;
import com.hq.fxh.Utils.Tool;

import java.util.List;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/8/20
 * @Email xlwenhao1858@sina.com
 */


public class RAdater_KanTieList extends RecyclerView.Adapter<RAdater_KanTieList.MyViewHolder> {
    List<PlateListModle.DataBean> data;

    public void setData(List<PlateListModle.DataBean> data) {
        this.data = data;
    }

    Context context;
    String url = "http://n.sinaimg.cn/news/1_img/upload/cf3881ab/20170820/dQL8-fykcpsc5592732.jpg";
    String url2 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1503466044888&di=2f15610a3af468eefe2ad27b15b44842&imgtype=0&src=http%3A%2F%2Fimg4.duitang.com%2Fuploads%2Fitem%2F201406%2F12%2F20140612211118_YYXAC.jpeg";
    String wurl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1503465623961&di=90bffc6eb7397e2f87d20234f7ab388e&imgtype=0&src=http%3A%2F%2Fpic.qiantucdn.com%2F58pic%2F14%2F88%2F34%2F52H58PICgrE_1024.jpg";
    KanTieCallBack kanTieCallBack;


    public RAdater_KanTieList(Context context, KanTieCallBack kanTieCallBack) {
        this.context = context;
        this.kanTieCallBack = kanTieCallBack;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LogUtils.e("onCreateViewHolder");
        MyViewHolder viewByViewType = new MyViewHolder(View.inflate(context, R.layout.item_kantie, null));
        return viewByViewType;

    }


    @Override
    public void onBindViewHolder(MyViewHolder viewHolder, final int position) {
        LogUtils.e("onBindViewHolder==" + position);

        PlateListModle.DataBean dataBean = data.get(position);

        List<String> title_pic = dataBean.getTitle_pic();
        int size = 0;
        if (title_pic != null) {
            size = title_pic.size();
        }

        if (size == 1) {
            viewHolder.layoutBBSMorePic.setVisibility(View.GONE);
            viewHolder.newsBigImage.setVisibility(View.VISIBLE);
            viewHolder.layoutBBS2Pic.setVisibility(View.GONE);

            String path = title_pic.get(0);
            LogUtils.e(path);

            GlideUtils.loadImageViewCen(context, path, viewHolder.newsBigImage);

        } else if (size == 3) {
            viewHolder.layoutBBSMorePic.setVisibility(View.VISIBLE);
            viewHolder.newsBigImage.setVisibility(View.GONE);
            viewHolder.layoutBBS2Pic.setVisibility(View.GONE);
            Tool.setUrlToImage(context, title_pic.get(0), viewHolder.newsImage1);
            Tool.setUrlToImage(context, title_pic.get(1), viewHolder.newsImage2);
            Tool.setUrlToImage(context, title_pic.get(2), viewHolder.newsImage3);


        } else if (size == 2) {
            viewHolder.layoutBBSMorePic.setVisibility(View.GONE);
            viewHolder.newsBigImage.setVisibility(View.GONE);
            viewHolder.layoutBBS2Pic.setVisibility(View.VISIBLE);
            Tool.setUrlToImage(context, title_pic.get(0), viewHolder.pci2Image1);
            Tool.setUrlToImage(context, title_pic.get(1), viewHolder.pci2Image2);

        } else {
            viewHolder.layoutBBSMorePic.setVisibility(View.GONE);
            viewHolder.newsBigImage.setVisibility(View.GONE);
            viewHolder.layoutBBS2Pic.setVisibility(View.GONE);
        }
        Tool.setStringToText(viewHolder.tvBBSAuthorName, dataBean.getMember_nickname());
        Tool.setUrlToImage(context, dataBean.getMember_header_url(), viewHolder.imageBBSAuthorIcon);
        Tool.setIntToText(viewHolder.readNewNum, dataBean.getNum_read());
        viewHolder.layoutBBSDrowDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                kanTieCallBack.onCall(position, (LinearLayout) view);

            }
        });
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {

        protected ImageView imageBBSAuthorIcon;
        protected TextView tvBBSAuthorName;
        protected LinearLayout layoutBBSDrowDown;
        protected TextView textView2;
        protected ImageView newsImage1;
        protected ImageView newsImage2;
        protected ImageView newsImage3;
        protected TextView textView3;
        protected RelativeLayout layoutBBSMorePic;
        protected ImageView pci2Image1;
        protected ImageView pci2Image2;
        protected LinearLayout layoutBBS2Pic;
        protected ImageView newsBigImage;
        protected TextView tvBBSPlateName;
        protected TextView readNewNum;
        protected TextView tvBBSCommentNum;

        public MyViewHolder(View itemView) {
            super(itemView);
            initView(itemView);
            LogUtils.e("viewHolder被创建" + itemView.toString());
        }


        private void initView(View rootView) {
            imageBBSAuthorIcon = (ImageView) rootView.findViewById(R.id.image_BBS_AuthorIcon);
            tvBBSAuthorName = (TextView) rootView.findViewById(R.id.tv_BBS_AuthorName);
            layoutBBSDrowDown = (LinearLayout) rootView.findViewById(R.id.layout_BBS_DrowDown);
            textView2 = (TextView) rootView.findViewById(R.id.tv_BBS_Tietle);
            newsImage1 = (ImageView) rootView.findViewById(R.id.news_image1);
            newsImage2 = (ImageView) rootView.findViewById(R.id.news_image2);
            newsImage3 = (ImageView) rootView.findViewById(R.id.news_image3);
            textView3 = (TextView) rootView.findViewById(R.id.textView3);
            layoutBBSMorePic = (RelativeLayout) rootView.findViewById(R.id.layout_BBS_MorePic);
            pci2Image1 = (ImageView) rootView.findViewById(R.id.pci2_image1);
            pci2Image2 = (ImageView) rootView.findViewById(R.id.pci2_image2);
            layoutBBS2Pic = (LinearLayout) rootView.findViewById(R.id.layout_BBS_2Pic);
            newsBigImage = (ImageView) rootView.findViewById(R.id.news_BigImage);
            tvBBSPlateName = (TextView) rootView.findViewById(R.id.tv_BBS_PlateName);
            readNewNum = (TextView) rootView.findViewById(R.id.readNewNum);
            tvBBSCommentNum = (TextView) rootView.findViewById(R.id.tv_BBS_CommentNum);
            int windowWidth = SystemUtils.getWindowWidth((Activity) context);
            int imageWidth = (windowWidth - DensityUtils.dip2px(context, 50)) / 3;
            setHeight(newsImage1, imageWidth, imageWidth);
            setHeight(newsImage2, imageWidth, imageWidth);
            setHeight(newsImage3, imageWidth, imageWidth);

            int imageWidth2 = (windowWidth - DensityUtils.dip2px(context, 40)) / 2;

            setHeight(pci2Image1, imageWidth2, imageWidth2);
            setHeight(pci2Image2, imageWidth2, imageWidth2);


            newsBigImage.setAdjustViewBounds(true);
            newsBigImage.setScaleType(ImageView.ScaleType.FIT_START);
            setHeight(newsBigImage, windowWidth - DensityUtils.dip2px(context, 30), 0);
            newsBigImage.setMinimumHeight(DensityUtils.dip2px(context, 150));
            newsBigImage.setMaxHeight(DensityUtils.dip2px(context, 250));


        }
    }

    public void setHeight(ImageView imageView, int width, int height) {

        ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
        if (height != 0) {
            layoutParams.height = height;

        }
        layoutParams.width = width;
        imageView.setLayoutParams(layoutParams);
    }

    public interface KanTieCallBack {
        void onCall(int position, LinearLayout layout);
    }

}
